package org.shoukaiseki.common.utils;

import org.apache.commons.lang3.BooleanUtils;
import org.shoukaiseki.common.exception.MessageVirtualException;

import java.util.Collection;

public class AssertMessageVirtualException {

    /**
     * 如果为null则抛 MessageVirtualException 错误
     * @param object
     * @param message
     * @param params
     */
    public static void notNull(Object object, String message, Object... params) {
        if (object == null) {
            throw new MessageVirtualException(message).format(params);
        }
    }

    /**
     * 如果为null或空字符空则抛 MessageVirtualException 错误
     * @param object
     * @param message
     * @param params
     */
    public static void notEmpty(Object object, String message, Object... params) {
        if (object == null) {
            throw new MessageVirtualException(message);
        }
        if(object instanceof String){
            if (StringUtils.isEmpty(object.toString())) {
                throw new MessageVirtualException(message).format(params);
            }
        }
    }


    /**
     * 全部为空则抛异常
     * @param message
     * @param objects
     */
    public static void allNullThrow(String message,Object... objects){
        if (objects==null) {
            throw new MessageVirtualException(message);
        }
        if (ObjectUtils.allNull(objects)) {
            throw new MessageVirtualException(message);
        }
    }

    /**
     * 任何一个为空则抛异常
     * @param message
     * @param objects
     */
    public static void anyNullThrow(String message,Object... objects){
        if (objects==null) {
            throw new MessageVirtualException(message);
        }
        if (ObjectUtils.anyNull(objects)) {
            throw new MessageVirtualException(message);
        }
    }

    /**
     * 全部为 true 则抛异常
     * @param message
     * @param objects
     */
    public static void allTrueThrow(String message,boolean... booleans){
        if (booleans==null||booleans.length==0) {
            return;
        }
        if(BooleanUtils.and(booleans)){
            throw new MessageVirtualException(message);
        }
    }


    /**
     * 任何一个为 true 则抛异常
     * @param message
     * @param objects
     */
    public static void anyTrueThrow(String message,boolean... booleans){
        if (booleans==null) {
            throw new MessageVirtualException(message);
        }
        for (boolean b : booleans) {
            if(b){
                throw new MessageVirtualException(message);
            }
        }
    }

    /**
     * {@code
     * 任何一个 无效 则抛异常
     * Long,Integer 类型,如果为0则异常
     * AssertMessageVirtualException.anyInvalidThrow(*)             =  正常
     * AssertMessageVirtualException.anyInvalidThrow(*, *)          =  正常
     * AssertMessageVirtualException.anyInvalidThrow(*, *,1)        =  正常
     * AssertMessageVirtualException.anyInvalidThrow('0', *)        =  正常
     * AssertMessageVirtualException.anyInvalidThrow(null)          =  throw
     * AssertMessageVirtualException.anyInvalidThrow(*, 0)          =  throw
     * AssertMessageVirtualException.anyInvalidThrow(*, null)       =  throw
     * AssertMessageVirtualException.anyInvalidThrow(*, *, null, *) =  throw
     * }
     * @param message
     * @param objects
     */
    public static void anyInvalidThrow(String message,Object... objects){
        if (objects==null) {
            throw new MessageVirtualException(message);
        }
        for (Object obj : objects) {
            if(obj==null){
                throw new MessageVirtualException(message);
            }
            if(obj instanceof Long&&obj.equals(0L)){
                throw new MessageVirtualException(message);
            }
            if(obj instanceof Integer&&obj.equals(0)){
                throw new MessageVirtualException(message);
            }
        }
    }

    /**
     * 任何一个为空则抛异常
     * @param message
     * @param objects
     */
    public static void allEmptyThrow(String message, Collection<?>... objects){
        if (ListUtils.anyNotEmpty(objects)) {
            throw new MessageVirtualException(message);
        }
    }


    /**
     * 任何一个为空则抛异常
     *
     * true throw
     * false 正常
     * <pre>
     * AssertMessageVirtualException.anyNotEmpty([])            = false
     * AssertMessageVirtualException.anyNotEmpty([1],[2])       = true
     * AssertMessageVirtualException.anyNotEmpty(null)          = false
     * AssertMessageVirtualException.anyNotEmpty(null, [])      = false
     * AssertMessageVirtualException.anyNotEmpty(null, [1])     = true
     * AssertMessageVirtualException.anyNotEmpty([1], null)     = true
     * AssertMessageVirtualException.anyNotEmpty([1],[2],null)  = true
     * </pre>
     *
     * @param message
     * @param objects
     * @return {@code false} if there is at least one {@code null} value in the array or the array is {@code null},
     * {@code true} if all values in the array are not {@code null}s or array contains no elements.
     * @since 3.5
     */
    public static void allEmptyThrow(String message, Object[]... objects){
        if (ListUtils.anyNotEmpty(objects)) {
            throw new MessageVirtualException(message);
        }
    }
}
